//package com.project.pudongdata.interceptor;
//
//
//import com.project.dto.R;
//import com.project.util.JSONUtils;
//import com.project.pudongdata.dto.SearchDto;
//import com.project.pudongdata.util.JwtUtil;
//
//import jakarta.servlet.http.HttpServletRequest;
//import jakarta.servlet.http.HttpServletResponse;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.web.servlet.HandlerInterceptor;
//import org.springframework.web.servlet.ModelAndView;
//
//import java.util.stream.Collectors;
//
//
//@Slf4j
//public class ControllerInterceptor implements HandlerInterceptor {
//
//    //controller之前
//    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception{
//        System.out.println("PreHandle invoke....");
//
//
////        // 使用 ContentCachingRequestWrapper 包装原始请求
////        ContentCachingRequestWrapper cachingRequest = new ContentCachingRequestWrapper((jakarta.servlet.http.HttpServletRequest) httpServletRequest);
////
////        // 获取请求体内容
////        byte[] buf = cachingRequest.getContentAsByteArray();
////        String bodyContent = new String(buf, 0, buf.length, cachingRequest.getCharacterEncoding());
//
//
//
////        // 获取原始的ServletInputStream
////        ServletInputStream inputStream = httpServletRequest.getInputStream();
////        // 读取请求体内容为字符串
////        StringBuilder stringBuilder = new StringBuilder();
////        String line;
////        try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
////            while ((line = reader.readLine()) != null) {
////                stringBuilder.append(line);
////            }
////        }
////        String bodyContent = stringBuilder.toString();
//        try {
//
//            String bodyContent = httpServletRequest.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
////            BufferedReader reader = httpServletRequest.getReader();
////            StringBuilder stringBuilder = new StringBuilder();
////            String line;
////            while ((line = reader.readLine()) != null) {
////                stringBuilder.append(line);
////            }
////            String bodyContent = stringBuilder.toString();
//            if (StringUtils.isNotBlank(bodyContent)) {
//
////            String requestBody = new String(buf, 0, buf.length, cachingRequest.getCharacterEncoding());
//                log.info("RequestBody: " + bodyContent);
//
//                SearchDto searchDto = JSONUtils.read(bodyContent, SearchDto.class);
//                //验证非空
//                if (StringUtils.isBlank(searchDto.getCode())) {
//                    httpServletResponse.getWriter().write(JSONUtils.write(R.fail("code不能为空")));
//                    return false;
//
//                }
//                //验证无效
//                if (!JwtUtil.checkToken(searchDto.getCode())) {
//                    httpServletResponse.getWriter().write(JSONUtils.write(R.fail("code无效或已过期")));
//                    return false;
//                }
//
//            }
////            else {
////                httpServletResponse.getWriter().write(JSONUtils.write(R.fail("参数不能为空")));
////                return false;
////            }
//        }catch (Exception e){
//            log.error("error on write json", e);
//        }
//
//        return true;
//    }
//    //post之前
//    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
//        System.out.println("postHandle被调用");
//
////                // 使用 ContentCachingRequestWrapper 包装原始请求
//////        ContentCachingRequestWrapper cachingRequest = new ContentCachingRequestWrapper( httpServletRequest);
//////
//////        // 获取请求体内容
//////        byte[] buf = cachingRequest.getContentAsByteArray();
//////        String bodyContent = new String(buf, 0, buf.length, cachingRequest.getCharacterEncoding());
////
////            BufferedReader reader = httpServletRequest.getReader();
////            StringBuilder stringBuilder = new StringBuilder();
////            String line;
////            while ((line = reader.readLine()) != null) {
////                stringBuilder.append(line);
////            }
////            String bodyContent = stringBuilder.toString();
////            if (StringUtils.isNotBlank(bodyContent)) {
////
//////            String requestBody = new String(buf, 0, buf.length, cachingRequest.getCharacterEncoding());
////                log.info("RequestBody: " + bodyContent);
////                SearchDto searchDto = JSONUtils.read(bodyContent, SearchDto.class);
////                //验证非空
////                if (StringUtils.isBlank(searchDto.getCode())) {
////                    httpServletResponse.getWriter().write(JSONUtils.write(R.fail("code不能为空")));
////                }
////                //验证无效
////                if (!JwtUtil.checkToken(searchDto.getCode())) {
////                    httpServletResponse.getWriter().write(JSONUtils.write(R.fail("code无效或已过期")));
////
////                }
////
////            } else {
////                httpServletResponse.getWriter().write(JSONUtils.write(R.fail("参数不能为空")));
////
////            }
//
//    }
//
//    //结束后调用
//    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
//        System.out.println("afterCompletion被调用");
//    }
//
//}
